<?php

class FileInfo
{
	// Private
	var $File;	// full path

	// Public
	var $Name;	// file name
	var $Type;	// file Type
	var $Size;	// file size
	var $Date;	// last update time
	var $Mode;	// file attrib

    var $Currdir;	// Currdir

    function setFile($File)
    {
    	$this->File = str_replace("\\", "/", $File);
    }

    function getFile()
    {
        return  $this->File;
    }

    function setName($Name)
    {
    	$this->Name = $Name;
    }

    function getName()
    {
    	return $this->Name;
    }

    function setType($Type)
    {
    	$this->Type = $Type;
    }

    function getType(){
		if(is_dir($this->File)){
			$Type = 'DIR';
		}else{
			preg_match  ("/^.+\.([a-z0-9]{0,})$/i",$this->File, $Ext);

			switch(strtolower($Ext[1])){
				case 'pcd'  :	case 'pcx'  :
				case 'jpge' :	case 'jpg'  :	case 'jpe'  :
				case 'gif'  :
				case 'png'  :
				case 'dib'  :	case 'rle'  :	case 'bmp'  :
				case 'tiff'	:	case 'tif'  :
					$Type = 'img';
				break;

				case 'vbs'  :	case 'vbse' :
				case 'js'   :	case 'jse'  :
					$Type = 'script';
				break;

				case 'exe'  :	case 'com' :
					$Type = 'binary';
				break;

				case 'ra'   :	case 'rm'   :	case 'rmj'  :	case 'rms'  :	case 'rmi'	:
				case 'ram'  :	case 'rmm'  :	case 'mnd'  :	case 'rp'   :	case 'rt'   :
				case 'mp4'	:	case 'rmvb' :
					$Type = 'realmedia';
				break;

				case 'mid'  :	case 'midi' :	case 'wav'  :	case 'wave' :
				case 'avi'  :	case 'aiff' :	case 'asf'  :	case 'au'   :	case 'wma'  :
				case 'mpeg'	:	case 'mpg'  :	case 'm3u'	:	case 'mov'  :
					$Type = 'msmedia';
				break;

				case 'mp3'	:	case 'mp2'  :	case 'mpv'  :	case 'mx3'  :
					$Type = 'mp3';
				break;

				case 'pdf'  :
					$Type = 'pdf';
				break;

				case 'rar'  :	case 'arj'  :	case 'tar'  :	case 'ace'  :
				case 'lzh'	:	case 'uue'  :	case 'bz2'  :
					$Type = 'rar';
				break;

				case 'zip'  :	case 'gzip' :	case 'cab'  :
				case 'tbz'  :	case 'tbz2' :
					$Type = 'zip';
				break;

				case 'css'	:	case 'sql'  :	case 'txt'  :
				case 'ini'	:	case 'inf'  :	case 'conf' :
				case 'cgi'  :   case 'inc'  :	case 'csv'	:
				case 'c'    :	case 'cpp'  :
					$Type = 'text';
				break;

				case 'rtf'  :	case 'doc'  :
					$Type = 'word';
				break;

				case 'swf'  :
					$Type = 'flash';
				break;

				case 'php'  :	case 'php3' :	case 'phtml' :
					$Type = 'php';
				break;

				case 'html' :	case 'htm'  :	case 'shtml' :
					$Type = 'html';
				break;

				case 'xml'  :
					$Type = 'xml';
				break;

				default     :
					$Type = 'other';
				break;
			}
		}

		$this->setType($Type);

		return $Type;
	}

    function setSize($Size)
    {
    	$this->Size = $Size;
    }

    function getSize()
    {
        $this->Size = ( $this->Type == 'DIR' ) ? 0 : filesize( $this->File );
        return $this->Size;
    }

    function setDate($Date)
    {
    	$this->Date = $Date;
    }

    function getDate()
    {
        $this->Date = filemtime($this->File);
        return $this->Date;
    }

    function setMode($Mode)
    {
    	$this->Mode = $Mode;
    }

	function getMode(){
		$Mode = fileperms($this->File);

			if($Mode & 0x1000) $Type='p'; // FIFO pipe
		elseif($Mode & 0x2000) $Type='c'; // Character special
		elseif($Mode & 0x4000) $Type='d'; // Directory
		elseif($Mode & 0x6000) $Type='b'; // Block special
		elseif($Mode & 0x8000) $Type='-'; // Regular
		elseif($Mode & 0xA000) $Type='l'; // Symbolic Link
		elseif($Mode & 0xC000) $Type='s'; // Socket
		else $Type='u'; // UNKNOWN

		// Determine les permissions par Groupe
		$Owner['r'] = ($Mode & 00400) ? 'r' : '-';
		$Owner['w'] = ($Mode & 00200) ? 'w' : '-';
		$Owner['x'] = ($Mode & 00100) ? 'x' : '-';
		$Group['r'] = ($Mode & 00040) ? 'r' : '-';
		$Group['w'] = ($Mode & 00020) ? 'w' : '-';
		$Group['e'] = ($Mode & 00010) ? 'x' : '-';
		$World['r'] = ($Mode & 00004) ? 'r' : '-';
		$World['w'] = ($Mode & 00002) ? 'w' : '-';
		$World['e'] = ($Mode & 00001) ? 'x' : '-';

		// Adjuste pour SUID, SGID et sticky bit

		if( $Mode & 0x800 ) $Owner['e'] = ($Owner['e']=='x') ? 's' : 'S';
		if( $Mode & 0x400 ) $Group['e'] = ($Group['e']=='x') ? 's' : 'S';
		if( $Mode & 0x200 ) $World['e'] = ($World['e']=='x') ? 't' : 'T';

		$Mode = "$Type$Owner[r]$Owner[w]$Owner[e]$Group[r]$Group[w]$Group[e]$World[r]$World[w]$World[e]";

        $this->setMode($Mode);

        return $Mode;

	}

    function setCurrDir($CurrDir)
    {
    	$this->CurrDir = $CurrDir;
    }

    function getCurrDir()
    {
    	return $this->CurrDir;
    }

    function FileInfo()
    {
        if ($GLOBALS['System']['Debug'])
		{
			$this->Debug = true;
		}
		else
		{
			$this->Debug = false;
		}
    }

    function getFileInfo($Path, $Name)
    {
        $this->setFile($Path.'/'.$Name);
		$this->setName($Name);

        $FileInfo['File'] = $this->getFile();
        $FileInfo['Name'] = $this->getName();
        $FileInfo['Type'] = $this->getType();
		$FileInfo['Size'] = $this->getSize();
		$FileInfo['Date'] = $this->getDate();
		$FileInfo['Mode'] = $this->getMode();

        return $FileInfo;
    }

    function getFolderInfo($CurrDir)
    {
        $this->setCurrDir($CurrDir);

        $Handle  = opendir($this->CurrDir);
		$File    = @readdir($Handle);      // repertoire .
		$File    = @readdir($Handle);      // repertoire ..

		while (false !== ($File = readdir($Handle))) {
            if(is_dir($this->CurrDir.'/'.$File))
            {
                $this->Dirs[] = $this->getFileInfo($this->CurrDir, $File);
			}
            else
            {
				$this->Files[] = $this->getFileInfo($this->CurrDir, $File);
			}
		}
		//usort($files,OrdDir);

        $FolderInfo['Dirs'] = $this->Dirs;
        $FolderInfo['Files'] = $this->Files;

		@closedir($handle);

        return $FolderInfo;
	}

}

?>